package userclass;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class UserUnit implements Serializable {

    private static Set<User>  userSet=new TreeSet<User>();
    public Set<User>  userSet1=new TreeSet<User>();

//	static{
//		initializeAdministrator( ) ;
//	}

    public static boolean isContain(Student s) {
        if(userSet.contains(s))
            return true;
        return false;
    }

    public Set<User> getUserSet() {
        return userSet;
    }

    public static void initializeAdministrator( ) {
        add(new Administrator("默认管理员", 0, "0"));
        add(new Student("默认学生", 1, "0"));
    }

    public static User peekUser( long ID ) {
        for(User u:userSet)
            if(u.getID()==ID)
                return u;
        return null;
    }

    public static ArrayList<User> peekUsers( String name ) {
        ArrayList<User> userList=new ArrayList<User>();
        for(User u:userSet) {
            if(u.getName()==name)
                userList.add(u);
        }

        return userList;
    }

    public static void add( User user ) {
        userSet.add(user);
    }

    public static void remove( User user ) {
        userSet.remove(user);
    }

    public static void display( PrintWriter out) {
        out.println("   姓名   \t\t\t学号");
        for(User u:userSet)
            out.println(u);
        out.println();
    }



    public void save()throws 	IOException{

        try(ObjectOutputStream output= new ObjectOutputStream(new FileOutputStream("UserUnit.txt"))){

            userSet1=userSet;
            output.writeObject(this);

        }
    }


    public static void load(  String fileName ) throws FileNotFoundException, IOException, ClassNotFoundException {

        try(ObjectInputStream input =  new  ObjectInputStream(new  FileInputStream("UserUnit.txt") )){

            UserUnit u =(UserUnit) input.readObject();
            userSet=u.userSet1;

        }
    }


}



